// pages/search/product/product.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgT:'http://delice.riki.cn/mavista/img/buy-top-bg.jpg',
    img:{
      travel:'http://delice.riki.cn/mavista/img/travel.png',
      pioneer:'http://delice.riki.cn/mavista/img/pioneer.png',
      explorer:'http://delice.riki.cn/mavista/img/explorer.png',
      adventurer:'http://delice.riki.cn/mavista/img/adventurer.png',
      leader:'http://delice.riki.cn/mavista/img/expedition%20Leader.png'
    },
    travel:'',
    note_bg:'http://delice.riki.cn/mavista/img/note-bg.png',
   

   
    toast: {
        toastTopColor: '#2ca9e0',
        toastTopText: '旅盒 1 · 去旅行',
        toastCons:[
          { toastImg: '../../../images/lx1.jpg', toastText: '旅行手册×1本' },
          { toastImg: '../../../images/lx2.jpg', toastText: '魔力闪卡×1套' },
          { toastImg: '../../../images/lx1.jpg', toastText: '特别礼物×1个' },
          { toastImg: '../../../images/lx1.jpg', toastText: '营养牛奶×6盒' },
          { toastImg: '../../../images/lx1.jpg', toastText: '毛绒公仔×1个' },
          { toastImg: '../../../images/lx1.jpg', toastText: '印花贴纸×1张' },
          { toastImg: '../../../images/lx1.jpg', toastText: '音频故事×1个' },
          { toastImg: '../../../images/lx1.jpg', toastText: '宝藏地图×1张' }
        ]
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('on show');
    let _this = this;
    let count = 10;
    clearInterval(_this.countTime)
    let authCode = {
      authCode: wx.getStorageSync("authCode")
    }
    app.requestData('/api/user/isLogin',authCode,'GET',function(res){
      console.log(res)
      if(!res.is_login){
        _this.setData({
            show1:true,
            show2:true
        })
        
      }
    })
    app.requestData('/api/purchase/order/contain_unpaid',authCode,'get',function(res){
      console.log(res)
      if (res.hasUnpaidOrder){
        
          // let count = 10
         
          _this.countTime =   setInterval(function () {
            
            if (count > 0) {

              count--;
              _this.setData({
                count: count,
                show3: true
              })
              console.log(count)

            } else {
              clearInterval(_this.countTime)
              wx.navigateTo({
                url: 'orderDetail/orderDetail',
              })
            }

          }, 1000)
        
      }
    }) 


    // app.requestData('/api/purchase/order/contain_unpaid', authCode, 'GET', function (success) {
    //   console.log(success)
    // },
    //   function (fail) {
    //     console.assert(fail)
    //   })
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        _this.setData({
          top: res.screenWidth * 26 / 100 - 20,
          height: res.screenHeight,
          width: res.screenWidth
        })
      }
    })
    let activityData = {}
    let price = {}

    app.requestData("/api/purchase/product/inventories", activityData, "POST",
      function (res) {
        console.log('getData-success');
        console.log(res);
        _this.setData({
          productsNum: res
        })
      },
      function (res) {
        console.log('getData-fail');
        console.log(res);
      }
    )
    app.requestData("/api/purchase/product/prices", price, "POST",
      function (res) {
        console.log('getData-success');
        console.log(res);
        _this.setData({
          prices: res
        })
      },
      function (res) {
        console.log('getData-fail');
        console.log(res);
      }
    )

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.countTime)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  radioChange(e){
    console.log(e)
    let _this = this;
    let value = e.currentTarget.dataset.val;
    let count = e.currentTarget.dataset.count;
    let toastData = _this.data.toastData;
    let price = e.currentTarget.dataset.price
    if(value=='travel'){
        _this.setData({
          travel:'checked',
          pioneer:'',
          explorer:'',
          adventurer:'',
          leader:''
        })
        _this.testIslogin(_this,count,price);
        
    } else if (value =='pioneer'){
      _this.setData({
        travel:'',
        pioneer: 'checked',
        explorer: '',
        adventurer: '',
        leader: ''
      })
      _this.testIslogin(_this, count,price);
    } else if (value == 'explorer') {
      _this.setData({
        travel: '',
        pioneer: '',
        explorer: 'checked',
        adventurer: '',
        leader: ''
      })
      _this.testIslogin(_this, count,price);
    } else if (value == 'adventurer') {
      _this.setData({
        travel: '',
        pioneer: '',
        explorer: '',
        adventurer: 'checked',
        leader: ''
      })
      _this.testIslogin(_this, count, price);
    } else if (value == 'leader') {
      _this.setData({
        travel: '',
        pioneer: '',
        explorer: '',
        adventurer: '',
        leader: 'checked'
      })
      _this.testIslogin(_this, count, price);
    }
  },
  userLogin(e){
    this.setData({
      show2:true
    })
  },
  userLoginBtn(e){
    let _this = this;
    let is_login = {
      authCode: wx.getStorageSync('authCode'),
          name: this.data.loginName,
          phone: this.data.loginPhone
        }
    app.requestData('/api/user/reg', is_login,"GET",function(res){
        console.log(res)
          if(res.status=='ok'){
              _this.setData({
                show2:false,
                show1:false
              })
          }
    })
  },
  closeBtn(e){
    this.setData({
      show2:false
    })
  },
  loginName(e){
    this.setData({
      loginName:e.detail.value
    })
  },
  loginPhone(e) {
    this.setData({
      loginPhone: e.detail.value
    })
  },
  orderDetail(){
    clearInterval(this.countTime)
    wx.navigateTo({
      url: 'orderDetail/orderDetail',
    })
  },
  testIslogin(_this, count, price){
   let authCode = {
     authCode: wx.getStorageSync("authCode")
   };
   app.requestData('/api/user/isLogin', authCode, 'GET', function (res) {
     console.log(res)
     if (!res.is_login) {
       _this.setData({
         show1: true,
         show2: true
       })

     } else {
         app.requestData('/api/purchase/order/contain_unpaid', authCode, 'GET', function (success) {
          console.log(success)
          if (success.hasUnpaidOrder){
           return
          }else {
            wx.navigateTo({
              url: 'gift/gift?count=' + count + '&price=' + price,
            })
          }
        },
          function (fail) {
            console.assert(fail)
          })
      
     }
   })
 }
})